﻿using FlyingEye.Come;
using FlyingEye.Consts;
using FlyingEye.DeviceMaintenance;
using Microsoft.EntityFrameworkCore;

namespace FlyingEye.EntityFrameworkCore
{
    public static class ComeModelBuilder
    {
        public static void OnModelCreating(ModelBuilder modelBuilder)
        {
            // COME 组配置
            modelBuilder.Entity<ComeGroupNotificationConfigurationModel>(builder =>
            {
                builder.ToTable(DeviceOpsServiceDbConsts.DbTablePrefix + "ComeGroupNotificationConfigurations",
                   DeviceOpsServiceDbConsts.DbSchema);

                // 设置从 COME 组到保养项目的一对多
                builder.HasMany<DeviceMaintenanceModel>()
                .WithOne()
                .HasForeignKey(e => e.ComeGroupNotificationConfigurationId)
                .IsRequired(false);

                builder.HasIndex(e => e.UniqueHashKey).IsUnique();
                builder.HasIndex(e => e.GroupName).IsUnique();
            });

            // COME 用户配置
            modelBuilder.Entity<ComeUserNotificationConfigurationModel>(builder =>
            {
                builder.ToTable(DeviceOpsServiceDbConsts.DbTablePrefix + "ComeUserNotificationConfigurations",
                  DeviceOpsServiceDbConsts.DbSchema);

                builder.HasMany<DeviceMaintenanceModel>()
                .WithOne()
                .HasForeignKey(e => e.ComeUserNotificationConfigurationId)
                .IsRequired(false);

                builder.HasIndex(e => e.UniqueHashKey).IsUnique();
                builder.HasIndex(e => e.Name).IsUnique();
            });

            // COME 用户
            modelBuilder.Entity<ComeUserModel>(builder =>
            {
                builder.ToTable(DeviceOpsServiceDbConsts.DbTablePrefix + "ComeUsers",
                  DeviceOpsServiceDbConsts.DbSchema);

                // 配置从用户到维护人员的一对多
                builder.HasMany<MaintenanceMemberModel>()
                .WithOne()
                .HasForeignKey(e => e.ComeUserId)
                .IsRequired();
            });
        }
    }
}
